package lemna.top.chunyu.batch.step3;

import java.util.Date;

import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemStreamReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.validation.BindException;

import lemna.top.chunyu.batch.config.ChunYunConfig;
import lemna.top.chunyu.batch.config.Constants;
import lemna.top.chunyu.batch.reader.CYFlatFileItemReader;
import lemna.top.chunyu.batch.step3.domain.WeiXiPaymentStatement;

@Configuration
public class Step3ReaderConfig {


	@Autowired
	ChunYunConfig config;

	@Autowired
	Resource step3File;
	
    @Bean 
    @StepScope
    public Resource step3File(@Value("#{jobParameters[date]}") Date date) {

		String dateParam = Constants.sdf1.format(date);
    	String filePath = config.getInputFilePath()+"weixin-"+dateParam+".csv";
        return new FileSystemResource(filePath);
    }

	@Bean(name = "Step3Reader")
	public CYFlatFileItemReader<WeiXiPaymentStatement> Step3Reader() {
		CYFlatFileItemReader<WeiXiPaymentStatement> reader = new CYFlatFileItemReader<WeiXiPaymentStatement>();
		reader.setResource(step3File);
//      reader.setResource(new FileSystemResource("d:/hu/cy/w.csv"));
		reader.setEncoding("GBK");
//      reader.setEncoding("utf-8");
		reader.setLinesToSkip(1);
		String[] comments = { "#" };
		reader.setComments(comments);
		reader.setLineMapper(new DefaultLineMapper<WeiXiPaymentStatement>() {
			{
				setLineTokenizer(new DelimitedLineTokenizer() {
					{
						setNames(new String[] { "transactionTime", "publicAccountID", "merchantNo", "subMerchantNo",
								"deviceNumber", "weChatOrderNo", "merchantOrderNo", "userIdentification",
								"transactionType", "transactionStatus", "payingBank", "currencyType", "totalAmount",
								"enterpriseRedAmount", "weChatRefundNo", "merchantRefundNo", "refundAmount",
								"enterpriseRedRefundAmount", "refundType", "refundStatus", "merchantName",
								"merchantDataPackage", "serviceCharge", "rate" });
					}
				});
				setFieldSetMapper(new FieldSetMapper<WeiXiPaymentStatement>() {

					@Override
					public WeiXiPaymentStatement mapFieldSet(FieldSet fieldSet) throws BindException {
						WeiXiPaymentStatement statement = new WeiXiPaymentStatement();

						statement.setTransactionTime(fieldSet.readDate("transactionTime", "yyyy-MM-dd HH:mm:ss"));
						statement.setPublicAccountID(fieldSet.readString("publicAccountID"));
						statement.setMerchantNo(fieldSet.readString("merchantNo"));
						statement.setSubMerchantNo(fieldSet.readString("subMerchantNo"));
						statement.setDeviceNumber(fieldSet.readString("deviceNumber"));
						statement.setWeChatOrderNo(fieldSet.readString("weChatOrderNo"));
						statement.setMerchantOrderNo(fieldSet.readString("merchantOrderNo"));
						statement.setUserIdentification(fieldSet.readString("userIdentification"));
						statement.setTransactionType(fieldSet.readString("transactionType"));
						statement.setTransactionStatus(fieldSet.readString("transactionStatus"));
						statement.setPayingBank(fieldSet.readString("payingBank"));
						statement.setCurrencyType(fieldSet.readString("currencyType"));
						statement.setTotalAmount(fieldSet.readDouble("totalAmount"));
						statement.setEnterpriseRedAmount(fieldSet.readDouble("enterpriseRedAmount"));
						statement.setWeChatRefundNo(fieldSet.readString("weChatRefundNo"));
						statement.setMerchantRefundNo(fieldSet.readString("merchantRefundNo"));
						statement.setRefundAmount(fieldSet.readDouble("refundAmount"));
						statement.setEnterpriseRedRefundAmount(fieldSet.readDouble("enterpriseRedRefundAmount"));
						statement.setRefundType(fieldSet.readString("refundType"));
						statement.setRefundStatus(fieldSet.readString("refundStatus"));
						statement.setMerchantName(fieldSet.readString("merchantName"));
						statement.setMerchantDataPackage(fieldSet.readString("merchantDataPackage"));
						statement.setServiceCharge(fieldSet.readDouble("serviceCharge"));
						statement.setRate(fieldSet.readDouble("rate"));

						return statement;
					}

				});
			}
		});
		return reader;
	}
}
